package org.worthydots.service.extractor;

import java.util.List;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.worthydots.access.jpa.dao.DAOFactory;
import org.worthydots.access.jpa.dao.StationDAO;
import org.worthydots.access.jpa.dao.StationRouteDAO;
import org.worthydots.access.jpa.dao.TrainDAO;
import org.worthydots.access.jpa.dao.TrainScheduleDAO;
import org.worthydots.access.jpa.entity.Station;
import org.worthydots.access.jpa.entity.StationRoute;
import org.worthydots.access.jpa.entity.Train;
import org.worthydots.access.jpa.entity.TrainSchedule;

public class ClearTripInfoExtractorService {

	private WebDriver driver;

	private StationDAO stationDAO;
	private TrainDAO trainDAO;
	private TrainScheduleDAO trainScheduleDAO;
	private StationRouteDAO stationRouteDAO;

	public ClearTripInfoExtractorService() {
		this.driver = new HtmlUnitDriver();
		stationDAO = DAOFactory.getInstance().getStationDAO();
		trainDAO = DAOFactory.getInstance().getTrainDAO();
		trainScheduleDAO = DAOFactory.getInstance().getTrainScheduleDAO();
		stationRouteDAO = DAOFactory.getInstance().getStationRouteDAO();
	}

	public void extractAndSaveAllStationInfo(boolean saveInfo) {
		driver.get("http://www.cleartrip.com/trains/stations/list");

		List<List<String>> rowsList = WebDriverUtil.getTableContents(driver, "id('ContentFrame')/div[1]/div/table");
		List<String> currentRow;
		String stationCode, stationName, stationLocation;
		double stationLatitude, stationLongitude;
		int numberOfTrains;
		Station station;
		for (int i = 0; i < rowsList.size(); i++) {
			currentRow = rowsList.get(i);
			if (currentRow.size() == 4) {
				stationCode = currentRow.get(0).toLowerCase();
				stationName = currentRow.get(1).toLowerCase();
				stationLocation = currentRow.get(2).toLowerCase();
				stationLatitude = 0.0;
				stationLongitude = 0.0;
				numberOfTrains = Integer.parseInt(currentRow.get(3));

				station = new Station(stationCode, stationName, stationLocation, stationLatitude, stationLongitude, numberOfTrains);

				if (saveInfo)
					stationDAO.saveStationIfNotPresent(station);
				else
					System.out.print("done: " + station);
			} else {
				System.out.println("skipped: " + currentRow);
			}
		}
	}

	public void extractAndSaveStationRoute(String stationCode, boolean saveInfo) {
		stationCode = stationCode.toLowerCase();

		driver.get("http://www.cleartrip.com/trains/stations/" + stationCode);

		List<List<String>> rowsList = WebDriverUtil.getTableContents(driver, "id('ContentFrame')/div[2]/div/table");
		List<String> currentRow;
		String parts[];
		String trainNumber, trainName;
		int arrivalTime, departureTime, hours, minutes;
		int stopTime;
		Boolean trainWeeklySchedule[] = new Boolean[7];
		Train train;
		TrainSchedule trainSchedule;
		StationRoute stationRoute;
		for (int i = 0; i < rowsList.size(); i++) {
			currentRow = rowsList.get(i);
			if (currentRow.size() == 11) {
				parts = currentRow.get(0).split("[(]");
				trainName = parts[0].trim().toLowerCase();
				trainNumber = parts[1].substring(0, parts[1].length() - 1).trim();

				parts = currentRow.get(1).split(":");
				hours = Integer.parseInt(parts[0]);
				minutes = Integer.parseInt(parts[1]);
				arrivalTime = (hours * 60) + minutes;

				parts = currentRow.get(2).split(":");
				hours = Integer.parseInt(parts[0]);
				minutes = Integer.parseInt(parts[1]);
				departureTime = (hours * 60) + minutes;

				parts = currentRow.get(3).split(" min");
				stopTime = Integer.parseInt(parts[0]);

				for (int j = 4; j < currentRow.size(); j++) {
					if (currentRow.get(j).equals("Y"))
						trainWeeklySchedule[j - 4] = true;
					else
						trainWeeklySchedule[j - 4] = false;
				}

				train = new Train(trainNumber, trainName);
				trainSchedule = new TrainSchedule(trainNumber, trainWeeklySchedule);
				stationRoute = new StationRoute(trainNumber, 0, stationCode, arrivalTime, departureTime, stopTime);

				if (saveInfo) {
					trainDAO.saveTrainIfNotPresent(train);
					trainScheduleDAO.saveTrainScheduleIfNotPresent(trainSchedule);
					stationRouteDAO.saveStationRouteIfNotPresent(stationRoute);
				} else {
					System.out.println("train: " + train);
					System.out.println("trainweeklyschedule: " + trainSchedule);
					System.out.println("stationroute: " + stationRoute);

					System.out.print("done: " + currentRow);
				}
			} else {
				System.out.println("skipped: " + currentRow);
			}
		}
	}

	public void extractTrainInfo(String trainNumber) {
		driver.get("http://www.cleartrip.com/trains/" + trainNumber);

		List<List<String>> rowsList = WebDriverUtil.getTableContents(driver, "id('ContentFrame')/div[2]/div/table");
		List<String> currentRow;
		for (int i = 0; i < rowsList.size(); i++) {
			currentRow = rowsList.get(i);
			if (currentRow.size() == 5) {
				// System.out.print("done: " + currentRow);
			} else {
				System.out.println("skipped: " + currentRow);
			}
		}
	}

	public void closeDriver() {
		driver.close();
	}
}
